﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TransCend.Util.Page
{
    /// <summary>
    /// 分页工具
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [Serializable]
    public class PageTools<T> : SimplePage, IPage
    {
        public PageTools()
        {

        }

        public PageTools(IList<T> datalist)
            : base(1, SimplePage.DEF_COUNT, datalist.Count())
        {
            
            this.list = datalist;
        }
        
        /// <summary>
        /// 获取分页工具
        /// </summary>
        /// <param name="pageNo">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="totalCount">记录总数</param>
        public PageTools(int pageNo, int pageSize, int totalCount)
            : base(pageNo, pageSize, totalCount)
        {
            
        }

        /// <summary>
        /// 获取分页工具
        /// </summary>
        /// <param name="pageNo">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="totalCount">记录总数</param>
        /// <param name="datalist">当前页记录</param>
        public PageTools(int pageNo, int pageSize, int totalCount, IList<T> datalist)
            : base(pageNo, pageSize, totalCount)
        {
            this.list = datalist;
        }

        /// <summary>
        /// 获取分页工具，获取当前页数据
        /// </summary>
        /// <param name="pageNo">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="dataList">全部记录</param>
        public PageTools(int pageNo, int pageSize, IList<T> dataList)
            : this(pageNo, pageSize, dataList.Count)
        {

            if (pageSize == 0)
            {
                this.List = dataList;
            }
            else
            {
                this.List = dataList.ToList<T>().FindAll(delegate(T info)
                {
                    int index = list.IndexOf(info);
                    if (index >= (pageNo - 1) * pageSize && index < pageNo * pageSize)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                });
            }
        }

        /// <summary>
        /// 当前页的数据
        /// </summary>
        private IList<T> list;

        /// <summary>
        /// 获取当前页的数据
        /// </summary>
        public IList<T> List
        {
            set { list = value; }
            get { return list; }
        }
       
    }
}
